package com.cssw.fyzb.job.executor.handler;

import com.cssw.fyzb.core.model.dodb.CmfUserVideo;
import com.cssw.fyzb.job.executor.config.BusiClient;
import com.cssw.fyzb.job.executor.config.TpsClient;
import com.cssw.fyzb.job.executor.model.tps.AuditResult;
import com.cssw.fyzb.job.executor.service.video.UserVideoService;
import com.cssw.fyzb.job.executor.util.CollectionUtil;
import com.cssw.fyzb.job.executor.util.StringUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 审核视频任务
 *
 * @author Feng Chen
 */
@Component
public class AuditVideoJob extends IJobHandler {

    @Autowired
    private UserVideoService userVideoService;

    @Autowired
    private TpsClient tpsClient;

    @Autowired
    private BusiClient busiClient;

    @Value("${video.audit.limit:200}")
    private int auditLimit;
    //通过
    @Value("${video.audit.adopt:90}")
    private int adopt;
    //疑似
    @Value("${video.audit.iDoubtIt:50}")
    private int iDoubtIt;

    //疑是
    private static final String AUDITRESULT_STATUS = "3";

    //拒绝
    private static final String REFUSE_STATUS = "2";

    //通过
    private static final String ADOPT_STATUS = "1";

    private static final Logger logger = LoggerFactory.getLogger(AuditVideoJob.class);

    @XxlJob("auditVideoJob")
    @Override
    public ReturnT<String> execute(String param) throws Exception {
        try {
            logger.info("limit : {}", auditLimit);
            //查询上传视频信息
            List<CmfUserVideo> videos = userVideoService.listWaitAuditVideo(auditLimit);
            if (CollectionUtil.isNotEmpty(videos)) {
                videos.forEach(v -> {
                    logger.info("Start audit vedio : {} ", v.getVideoId());
                    AuditResult auditResult = tpsClient.getAuditResult(v.getVideoId());
                    logger.info("auditResult : {} ", auditResult);
                    if (auditResult != null && StringUtil.isNotEquals(auditResult.getStatus(), "0")) {
                        try {
                            logger.info("score : {} ", auditResult.getScore());
                            if (auditResult.getScore() > adopt) {
                                userVideoService.auditVideo(v.getId(), v.getVideoId(), Integer.valueOf(ADOPT_STATUS));
                                busiClient.getAddVideoEs(v.getId());
                                logger.info("审核通过： " + auditResult.getScore());
                            } else if (auditResult.getScore() > iDoubtIt) {
                                userVideoService.auditVideo(v.getId(), v.getVideoId(), Integer.valueOf(AUDITRESULT_STATUS));
                                logger.info("疑似 ：" + auditResult.getScore());
                            } else {
                                userVideoService.auditVideo(v.getId(), v.getVideoId(), Integer.valueOf(REFUSE_STATUS));
                                logger.info("不通过 ：" + auditResult.getScore());
                            }
                        } catch (Exception e) {
                            logger.error("", e);
                        }
                    }
                    logger.info("Finish audit vedio : {} ", v.getVideoId());
                });
            }
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            logger.error("", e);
            return ReturnT.FAIL;
        }
    }


}
